//
//  DHCartTableViewCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCartTableViewCell.h"
#import "DHProductDetailViewController.h"

@interface DHCartTableViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;
@property (weak, nonatomic) IBOutlet YYLabel *nameLabel;

@property (strong, nonatomic) NSDictionary *cartDic;
@property (strong, nonatomic) NSDictionary *sizeDic;
@property (weak, nonatomic) IBOutlet UILabel *stockoutLabel;

@property (strong, nonatomic) NSString *prodictID;
@property (strong, nonatomic) NSString *brandName;

@end

@implementation DHCartTableViewCell

- (IBAction)productDetailAction:(id)sender {
    if (self.prodictID) {
        DHProductDetailViewController *detailController = [[DHProductDetailViewController alloc] initWithProductID:self.prodictID brandName:self.brandName];
        [self.viewController.navigationController pushViewController:detailController animated:YES];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    WeakSelf(weakSelf)
    [self.selectButton bk_addEventHandler:^(UIButton *sender) {
        if (weakSelf.stockoutLabel.hidden) {
            sender.selected = !sender.selected;
            if (sender.selected) {
                [sender setImage:[UIImage imageNamed:@"pay_selected"] forState:UIControlStateNormal];
                if (weakSelf.sizeDic) {
                    [weakSelf routerEventWithName:@"DHCartTableViewCell" userInfo:@{@"cartDic":weakSelf.cartDic,@"sizeDic":weakSelf.sizeDic,@"state":@"1"}];
                }
            }else{
                [sender setImage:[UIImage imageNamed:@"pay_unselected"] forState:UIControlStateNormal];
                if (weakSelf.sizeDic) {
                    [weakSelf routerEventWithName:@"DHCartTableViewCell" userInfo:@{@"cartDic":self.cartDic,@"sizeDic":weakSelf.sizeDic,@"state":@"0"}];
                }
            }
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)dic{
    self.cartDic = dic;
    Product *produt = [Product modelWithDictionary:[dic objectForKey:@"product"]];
    self.prodictID = produt._id;
    self.brandName = produt.brand.name? : @"商品详情";
    
    self.sizeLabel.text = [dic objectForKey:@"selected_size_name"];
    if (produt.snapshots.count > 0) {
        [self.imgView dh_setImageURLString:produt.snapshots[0]];
    }else{
        [self.imgView dh_setImageURLString:@""];
    }
    self.nameLabel.text = produt.name;
    
    __weak typeof(self)weakSelf = self;
    [produt.sizes enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([[obj objectForKey:@"_id"] isEqualToString:[dic objectForKey:@"selected_size_id"]]) {
            weakSelf.sizeDic = obj;
            weakSelf.priceLabel.text = [NSString stringWithFormat:@"￥%@",[obj objectForKey:@"lease_price"]];
            *stop = YES;
        }
    }];
    if (produt.sizes.count > 0) {
        NSDictionary *sizeDic = produt.sizes[0];
        if ([[sizeDic objectForKey:@"inventory"] integerValue] - [[sizeDic objectForKey:@"locked"] integerValue] <= 0) {
            self.stockoutLabel.hidden = NO;
        }else{
            self.stockoutLabel.hidden = YES;
        }
    }
}

- (void)updateCellFromOrderManager:(NSDictionary *)dic{
    self.prodictID = [dic objectForKey:@"_id"];
    self.brandName = [[dic objectForKey:@"brand"] objectForKey:@"name"];
    
    NSArray *snapshots = [dic objectForKey:@"snapshots"];
    if (snapshots.count > 0) {
        [self.imgView dh_setImageURLString:snapshots[0]];
    }else{
        [self.imgView dh_setImageURLString:@""];
    }
    self.nameLabel.text = [dic objectForKey:@"name"];
    
    NSArray *sizes = [dic objectForKey:@"sizes"];
    if (sizes.count > 0) {
        NSDictionary *sizeDic = sizes[0];
        self.sizeLabel.text = [sizeDic objectForKey:@"size"];
        if ([[sizeDic objectForKey:@"inventory"] integerValue] - [[sizeDic objectForKey:@"locked"] integerValue] <= 0) {
            self.stockoutLabel.hidden = NO;
        }else{
            self.stockoutLabel.hidden = YES;
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
